package main

import (
	"base-service/routers"
	"base-service/utils"
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	"time"
)

func main() {
	env := utils.GetConfigString("base.environment")
	if len(env) > 0 {
		// 获取默认的 gin Engine，Engine 中包含了所有路由处理的接口
		gin.SetMode(env)
	}
	r := gin.Default()

	r.Use(cors.New(Cors()))
	//开启 http2
	r.UseH2C = true
	routers.BaseRouter(r)

	// 监听端口默认为8080
	r.Run(":8085")

}

func Cors() cors.Config {
	return cors.Config{
		AllowOrigins: []string{
			"http://localhost:3078",
			"https://demo.anjiesoft.com"}, // 明确指定允许的来源
		AllowMethods: []string{"GET", "POST", "PUT",
			"PATCH", "DELETE", "HEAD", "OPTIONS"},
		AllowHeaders: []string{"Origin", "Content-Length", "Content-Type",
			"Authorization", "Refresh_Authorization", "Project", "Auth"},
		ExposeHeaders:    []string{"Content-Length"},
		AllowCredentials: true,
		MaxAge:           12 * time.Hour,
	}
}
